local Msg = require("core.Msg")
local Human = require("core.Human")
local Map = require("core.Map")
local MapExcel = require("excel.Map").scene
local DailyActFW = require("activity.DailyActFW")
local ActDefine = require("activity.Define")
local FamilyManager = require("family.FamilyManager")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local RiotRewardExcel = require("excel.ArmyRiot").reward
local MailLogicFW =require("mail.MailLogicFW")
local Lang = require("common.Lang")
local Grid = require("bag.Grid")
local FamilyLogicFW = require("family.FamilyLogicFW")

FAMILY_INTEGRAL = FAMILY_INTEGRAL or {}
--{[1] = {familyUuid,familyName,integral}}


function sendRiotInfo(human)
    local sendMsg = Msg.gc.GC_ARMY_RIOT_INFO
    sendMsg.myFamily_rank = 0
    sendMsg.integral = 0
    sendMsg.contribution = 0
    sendMsg.exp = 0
    local endTime = DailyActFW.onGetCurRunningActEndTime(ActDefine.ACT_TYPE_ARMY_RIOT)
    sendMsg.remainTime = endTime - os.time() > 0 and endTime - os.time() or 0
    local len = #FAMILY_INTEGRAL < 5 and #FAMILY_INTEGRAL or 5
    sendMsg.rank[0] = len 
    for i=1, len do
        sendMsg.rank[i].familyUuid = FAMILY_INTEGRAL[i].familyUuid
        sendMsg.rank[i].familyName = FAMILY_INTEGRAL[i].familyName
        sendMsg.rank[i].rank = i
        sendMsg.rank[i].integral = FAMILY_INTEGRAL[i].integral or 0
    end
    for i=1, #FAMILY_INTEGRAL do
        if human.familyUuid == FAMILY_INTEGRAL[i].familyUuid then
            sendMsg.myFamily_rank = i > 5 and 0 or i
            sendMsg.integral = FAMILY_INTEGRAL[i].integral or 0
            sendMsg.contribution = human.db.riotContribution
            sendMsg.exp = human.db.riotFamilyExp
        end
    end
    Msg.send(sendMsg, human.fd)
end
function sortFunc(a, b)
    if a.integral > b.integral then
        return true
    end
end
--设置军团积分
function setRiotRankInfo(human, oType, info)
    local isHave = false
    for i=1, #FAMILY_INTEGRAL do
        if FAMILY_INTEGRAL[i].familyUuid == human.familyUuid then
            FAMILY_INTEGRAL[i].integral = FAMILY_INTEGRAL[i].integral + info.integral
            isHave = true
        end
    end
    if not isHave and human.familyUuid ~= "" then
        FAMILY_INTEGRAL[#FAMILY_INTEGRAL + 1] = {}
        FAMILY_INTEGRAL[#FAMILY_INTEGRAL].familyUuid = human.familyUuid
        FAMILY_INTEGRAL[#FAMILY_INTEGRAL].familyName = human.familyName
        FAMILY_INTEGRAL[#FAMILY_INTEGRAL].integral = info.integral
    end
end

--设置增加的军团经验
function setRiotFamilyExp(human, val)
    if DailyActFW.onCheckActIsRunning(ActDefine.ACT_TYPE_ARMY_RIOT) then
        human.db.riotFamilyExp = human.db.riotFamilyExp + val
    end
end

--定时发送活动面板显示信息
function sendRiotRank()
    table.sort(FAMILY_INTEGRAL, sortFunc)  
    if not DailyActFW.onCheckActIsRunning(ActDefine.ACT_TYPE_ARMY_RIOT) then
        return
    end
    for k,v in pairs(Human.onlineHuman) do
        if v.scene_id and Map.scene_id_to_map_id[v.scene_id] and MapExcel[Map.scene_id_to_map_id[v.scene_id] ].mapType == 0 and v.db.lv >= 37 then
            sendRiotInfo(v)
        end
    end
end

function onActivityStart()
  FAMILY_INTEGRAL = {}
end

function onActivityEnd()
    --发送奖励
    local len = #FAMILY_INTEGRAL
    local nRewardLen = #FAMILY_INTEGRAL < 5 and #FAMILY_INTEGRAL or 5
    local sendMsg = Msg.gc.GC_ARMY_RIOT_REWARD_INFO  
    local cache = {}
    for i=1, len do
        local nGridList = {}
        if i <= nRewardLen then
            sendMsg.item[0] = #RiotRewardExcel[i].item
            for j=1, #RiotRewardExcel[i].item do
                nGridList[j] = Grid.create(nil, RiotRewardExcel[i].item[j][1], RiotRewardExcel[i].item[j][2], 1)
                Grid.create(cache, RiotRewardExcel[i].item[j][1], RiotRewardExcel[i].item[j][2])
                Grid.makeItem(cache, sendMsg.item[j], 0)      
            end
        end
        local str = string.format(Lang.RIOT_REWARD_MAIL_CONTENT, i)    
        local family = FamilyManager.findByUuid(FAMILY_INTEGRAL[i].familyUuid)
        for _, member in ipairs(family.memberList) do
            local target = Human.onlineUuid[member.uuid]
            if target and target._id and target.db.riotFamilyExp > 0 and target.db.riotContribution > 0 and i <= nRewardLen then
                sendRiotInfo(target)
                local content = string.format(Lang.RIOT_REWARD_MAIL_CONTENT, i)
                MailLogicFW.addSystemMessage(target._id, Lang.RIOT_REWARD_MAIL_TITLE, content, nGridList)  
                Msg.send(sendMsg, target.fd)  
            end
            if target and target._id and target.db.riotContribution > 0 then
                FamilyLogicFW.setFamilyContribution(target, target.db.riotContribution)
                FamilyLogicFW.sendContribution(target, family)
            end
        end
        if i<= nRewardLen then
          FamilyLogicFW.addFamilyExp(family, RiotRewardExcel[i].addExp, true)
        end  
    end
    --通知前端结束活动
    for k,v in pairs(Human.onlineHuman) do
        local sendMsg = Msg.gc.GC_ARMY_RIOT_END
        Msg.send(sendMsg, v.fd)
    end
    --清空数据
    FAMILY_INTEGRAL = {}
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_ARMY_RIOT, setRiotRankInfo)
